package cn.jdy.anquan.service;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.SQLQuery;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import cn.jdy.anquan.entity.Report;
import cn.jdy.anquan.entity.Survey;
import cn.jdy.framework.db.hibernate.BaseDao;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.plugins.entity.HelpInfo;

@Service
public class SurveyService extends BaseDao<Survey> {
  public List<Survey> query(){
    List<Survey> list = new ArrayList<Survey>();
    SQLQuery query = super.em().createSQLQuery("select id,createTime,ip, result,count(1) total from survey group by result order by result");
    query.addEntity(Survey.class);
    list = query.list();
    return list;
  }
  
  /**
   * @param bo
   * @return
   */
  public int getCount(){
    try {
      DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
      return super.count(dc).intValue();
    } catch (Exception e) {
      BaseLog.e(this.getClass(), "查询投票记录数！", e);
    }
    return 0;
  }
  
  /**
   * 按条件查询
   * @param helpInfo
   * @return
   */
  public List<Survey> query(Survey obj) {
    if(obj == null) return super.findAll();
    List<Survey> list = null;
    DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
    if(obj.getiDisplayLength() > 0){
      list = super.find(dc,obj.getiDisplayStart(),obj.getiDisplayLength());
    }else{
      list = super.find(dc);
    }
    return list;
  }
  
  public Long count(Survey obj) {
    DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
    return super.count(dc);
  }
}
